// =====================================================================================
// Copyright ?2006 SmartCodeGenerator created by Shahed Khan. 
/*
//Microsoft Shared Source Community License (MS-CL)
//Published: October 18, 2005
*/
// If you like this code then feel free to go ahead and use it.
// Your use of this software is entirely at your own risk.
// I make no claims or warrantees about the reliability or fitness of this code for any particular purpose.
// website www.smartcodegenerator.com, email shahed.khan@gmail.com
// =====================================================================================

using System;
using System.Collections.Generic;
using System.Text;
using System.Data;

namespace SmartCodeGen.Providers
{
    [Serializable]
    public class ColumnSchema: DataObjectBase
    {
        // Methods
        public ColumnSchema(TableSchema table, string name, DbType dataType, string nativeType, int size, byte precision, int scale, bool allowDBNull)
        {
            this.table = table;
            base.connectionString = table.ConnectionString;
            base.databaseSchemaProvider = table.DatabaseSchemaProvider;
            base.name = name;
            base.dataType = dataType;
            base.nativeType = nativeType;
            base.size = size;
            base.precision = precision;
            base.scale = scale;
            base.allowDBNull = allowDBNull;
        }
        public ColumnSchema(TableSchema table, string name, DbType dataType, string nativeType, int size, byte precision, int scale, bool allowDBNull, ExtendedPropertyCollection extendedProperties)
            : this(table, name, dataType, nativeType, size, precision, scale, allowDBNull)
        {
            base.extendedProperties = extendedProperties;            
        }
        
        // Properties
        public bool IsForeignKeyMember
        {
            get
            {
                for (int num1 = 0; num1 < this.Table.ForeignKeys.Count; num1++)
                {
                    if (this.Table.ForeignKeys[num1].ForeignKeyMemberColumns.Contains(this))
                    {
                        return true;
                    }
                }
                return false;

            }
        }
        public bool IsPrimaryKeyMember
        {
            get
            {
                if (this.Table.PrimaryKey != null)
                {
                    return this.Table.PrimaryKey.MemberColumns.Contains(this.Name);
                }
                return false;

            }
        }
        public bool IsUnique
        {
            get
            {
                for (int num1 = 0; num1 < this.Table.Indexes.Count; num1++)
                {
                    if ((this.Table.Indexes[num1].IsUnique && (this.Table.Indexes[num1].MemberColumns.Count == 1)) && this.Table.Indexes[num1].MemberColumns.Contains(this))
                    {
                        return true;
                    }
                }
                return false;
            }
        }
        private TableSchema table;
        public TableSchema Table
        {
            get
            {
                return this.table;
            }
        }

        #region Custom Properties for convenience
        
        private string _ColumnAliasName;

        //Returns AliasName if set
        public string ColumnAliasName
        {
            get
            {
                return _ColumnAliasName;
            }
            set { _ColumnAliasName = value; }
        }


        public string PropertyName
        {
            get { return SchemaHelper.GetPropertyName(this); }
        }       

        public string CSDefaultByType
        {
            get { return SchemaHelper.GetCSDefaultByType(this); }
        }



        #endregion

        

    }
}
